package zjw;

import java.util.Map;

import org.apache.velocity.VelocityContext;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.VelocityUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class HttpClientVe extends Application {

	static int size = 0;

	@Override
	public void start(Stage stage) throws Exception {

		// stage.setTitle("剪贴板");

		// 创建场景并创建场景中UI控件的根节点。
		// BorderPane pane = new BorderPane();
		VBox vBox = new VBox();
		// pane.setCenter(vBox);
		Insets value = new Insets(0, 10, 0, 0);

		Scene scene = new Scene(vBox, 300, 150);
		stage.setX(600);
		stage.setScene(scene);
		Text text = new Text();
		Label label = new Label("Enter your name:");
		label.setStyle("-fx-text-fill: blue;");
		TextField textField = new TextField("");
		vBox.setMargin(vBox, value);
		textField.setMaxSize(256, 32);
		// vBox.getChildren().add(label);
		vBox.getChildren().add(textField);
		vBox.getChildren().add(text);
		Button exit = new Button("Exit");
		Button submit = new Button("submit");
		submit.setOnAction(event -> {
			String s = test(textField.getText());
			if ("success".equals(s)) {
				System.exit(0);
			} else
				text.setText(s);
		});
		exit.setOnAction(event -> {
			Platform.exit();
			//System.exit(0);
		});
		vBox.getChildren().add(submit);
		vBox.getChildren().add(exit);
		vBox.setSpacing(15);
		stage.show();
	}

	public static void main(String[] args) {
		// JavaFX中main函数必须需要调用launch函数
		launch(args);

		// System.out.println(strc("delAppRecomshopPc","del%app%sh"));
	}

	public static String strc(String sttt, String teststr) {

		String[] ts = teststr.split("%");
		for (int j = 0; j < ts.length; j++) {
			if (StrUtil.containsIgnoreCase(sttt, ts[j])) {
				if (j == ts.length - 1) {
					return sttt;
				}
			} else {
				return null;
			}
		}
		return null;
	}

	// static String sttt =
	// "delAppRecomshopPc,delCustomerService,delDicEnterfile,delAppRecomcontentPc,getContact,delPowerLabel,getDetailed,delShopInfo,delRole,DelDuty,delAppEntryPc,delAppChannelPc,getProductScreening,getProductScreeningLabel,getRefresh,getProduct,delAppPushgoodsPc,delAppRecomclassPc,getStrength,getEnterpByKey,getSubmenuOfId,{id},delSysAppmenu,getDetailedEvaluation,getShopApproval,delAppRecomblendPc,delAppRecomPc,getStoreShelves,getContactByEnterpinfoid,getExamination,delAppPushshopPc,getMessageDetail,delShopHonor,delMenu,getEditorLogo,getRoleDetail,getShopName,delAppPushcontentPc,delDicEnterptype,getEditService,delRole,getUserDetails,getContact,delDicShoptype,delAppClassitemPc,getContent,getContentlabelid,delAppClasssetPc,delDicApptemplate,delAppRelease,delShopGoodstype,delAppPushlistPc,delAppChannelcontentPc,getCommoditySecondary,delLogo,delAppRecomsubclassPc,getCompanyDetails,getRoleOfUser,delUser,{id},getRoleMenu";

	public static String test(String apiName) {
		try {
			String str = JSONUtil.toJsonStr(HttpUtil.get("http://localhost:88/v2/api-docs/"));
			Map<String, Object> map1 = JSONUtil.parseObj(str);
			Map<String, Object> map2 = (Map<String, Object>) map1.get("paths");
			String apiStr = JSONUtil.toJsonStr(map2.keySet())
					.substring(1, JSONUtil.toJsonStr(map2.keySet()).indexOf("]")).replace("\"", "");
			String apiArr[] = apiStr.split(",");
			VelocityContext context = new VelocityContext();
			for (int i = 0; i < apiArr.length; i++) {
				if (apiArr[i].substring(apiArr[i].lastIndexOf("/") + 1, apiArr[i].lastIndexOf("/") + 2).equals("{")) {
					String[] ss = apiArr[i].split("/");
					if (strc(ss[3], apiName) != null) {
						context.put("url", apiArr[i]);
						context.put("method", "get");
						// strc("del%content%pc");
					}
				} else if (strc(apiArr[i].substring(apiArr[i].lastIndexOf("/") + 1), apiName) != null) {
					context.put("url", apiArr[i]);
					context.put("method", "post");
				}
			}
			Map<String, Object> map3 = (Map<String, Object>) JSONUtil
					.parse(((Map<String, Object>) JSONUtil.parse(map2.get(context.get("url")))).get("post"));
			String p = JSONUtil.toJsonStr(map3.get("parameters"));
			String entity = p.substring(p.indexOf("definitions/") + 12, p.indexOf("\"}"));
			context.put("entity", entity);
			VelocityUtil.init("D:\\develop\\code\\workspace\\myspringboottest\\src\\main\\resources\\templates\\",
					"UTF-8");
			VelocityUtil.toFile("httpclient.java.vm", context,
					"D:\\develop\\code\\workspace\\myspringboottest\\src\\main\\java\\zjw\\controller\\Httpclient.java");
		} catch (Exception e) {
			return "error";
		}
		return "success";
	}
}
